package org.microframework.base.feign.system.factory;

import org.microframework.base.core.domain.R;
import org.microframework.base.feign.system.RemoteUserService;
import org.microframework.base.model.system.domain.User;
import org.microframework.base.model.system.dto.SecurityUser;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户服务降级处理
 */
@Component
@Slf4j
public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserService> {

	@Override
	public RemoteUserService create(Throwable throwable) {
		log.error("用户服务调用失败:{}", throwable.getMessage());
		return new RemoteUserService() {
			@Override
			public R<SecurityUser> getUserInfo(String username, String source) {
				return R.fail("获取用户失败:" + throwable.getMessage());
			}

			@Override
			public R<Boolean> registerUserInfo(User sysUser, String source) {
				return R.fail("注册用户失败:" + throwable.getMessage());
			}

			@Override
			public R<Boolean> recordUserLogin(User sysUser, String source) {
				return R.fail("记录用户登录信息失败:" + throwable.getMessage());
			}

			@Override
			public R<SecurityUser> loadUserByUsername(String username, String source) {
				return R.fail("通过用户名加载用户信息失败:" + throwable.getMessage());
			}
		};
	}
}
